import types from '@/store/mutations/mutation-types';

const MESSAGE_DEFAULT = {
    success: '操作成功！',
    error: '操作失败！'
};

/**
 * 提交全局Message，在App.vue中有注册监听
 * @param {Object} commit 
 * @param {Object} res 
 * @param {Object} payload 
 * @returns 
 */
 function commitGlobalMessage(commit, res, payload) {
	let status = res.status || {};
	const { message, txtSuccess, txtError } = payload;
	let txt = '';
	let type = 'success';
	// 关掉全局 message，不弹出全局message提示
	if(message === false) {
		return;
	}
	if(status.code === 200) {
		txt = txtSuccess || status.desc || MESSAGE_DEFAULT.success;
	} else {
		type = status.code === -1 ? 'info' : 'warning';
		txt = txtError || status.desc || MESSAGE_DEFAULT.error;
	}
    if(![100000, 30020017].includes(status.code)) {
		commit(types.MESSAGE, { message: txt, type });
	}
}


const ResonpseHandler = {
    // 防抖错误处理
    1: function (commit, dispatch, state, res, payload) {},
    // 请求结果正常状态码
    200: function (commit, dispatch, state, res, payload) {
        
    },
    // 账号互顶错误码
    100000: function (commit, dispatch, state, res, payload) {
		// 账号互顶
		commit(types.META, {
			topAccountError: {
				visible: true,
				data: res.data,
				desc: res.status.message
			}
		})
	},
    // 未登录错误码
    20100001: function (commit, dispatch, state, res, payload) {
		// 用户登录失效
		dispatch('logout')
    },
    30020017: function (commit, dispatch, state, res, payload) {
		// 被禁用
		commit(types.MESSAGE, { message: '抱歉，您没有权限访问平台奥~如有需要，请联系平台管理员。', type: 'error' });
    },
    // 服务器异常：似乎出了什么状态，正在恢复中...
    40000002: function (commit, dispatch, state, res, payload) {}
};

export {
    commitGlobalMessage
};
export default ResonpseHandler;